Getting Started |
您所在的位置:网站首页 › view routes › Getting Started |
Getting Started Watch a Free Vue Router Video Course Creating a Single-page Application with Vue + Vue Router feels natural: with Vue.js, we are already composing our application with components. When adding Vue Router to the mix, all we need to do is map our components to the routes and let Vue Router know where to render them. Here's a basic example: HTML html Hello App! Go to Home Go to About Hello App! Go to Home Go to About router-link Note how instead of using regular a tags, we use a custom component router-link to create links. This allows Vue Router to change the URL without reloading the page, handle URL generation as well as its encoding. We will see later how to benefit from these features. router-view router-view will display the component that corresponds to the URL. You can put it anywhere to adapt it to your layout. ![]() By calling app.use(router), we are triggering the initial navigation and giving access to this.$router as well as the current route as this.$route inside of any component: js// Home.vue export default { computed: { username() { // We will see what `params` is shortly return this.$route.params.username }, }, methods: { goToDashboard() { if (isAuthenticated) { this.$router.push('/dashboard') } else { this.$router.push('/login') } }, }, }// Home.vue export default { computed: { username() { // We will see what `params` is shortly return this.$route.params.username }, }, methods: { goToDashboard() { if (isAuthenticated) { this.$router.push('/dashboard') } else { this.$router.push('/login') } }, }, }To access the router or the route inside the setup function, call the useRouter or useRoute functions. We will learn more about this in the Composition API Throughout the docs, we will often use the router instance. Keep in mind that this.$router is exactly the same as directly using the router instance created through createRouter. The reason we use this.$router is because we don't want to import the router in every single component that needs to manipulate routing. |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |